package it.mrcrack.model;

import it.mrcrack.exception.DataFormatException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * Classe che estende java.util.properties per usare un costruttore che accetti il path di un file
 * @author Mena
 *
 */
public class Properties extends java.util.Properties {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1414815315253284736L;

	public Properties(String path) throws IOException{
		super();
		f=new File(path);
		if(f.createNewFile())
			if(!f.isFile()){
				f.delete();
				throw new DataFormatException(); -------------------
 			}
		FileInputStream fi=new FileInputStream(f);
		load(fi);
		fi.close();
	}
	
	public Object setProperty(String key, String value){
		Object o=super.setProperty(key, value);
		FileOutputStream fo;
		try {
			fo = new FileOutputStream(f);
			store(fo, "");
			fo.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o;
	}
	
	File f;
}
